package com.qimu.saiyou.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qimu.saiyou.common.BaseResponse;
import com.qimu.saiyou.common.ErrorCode;
import com.qimu.saiyou.common.ResultUtils;
import com.qimu.saiyou.exception.BusinessException;
import com.qimu.saiyou.model.dto.joincompete.JoinCompeteRequest;
import com.qimu.saiyou.model.entity.Competition;
import com.qimu.saiyou.model.entity.User;
import com.qimu.saiyou.model.vo.CompetitionVO;
import com.qimu.saiyou.service.CompetitionService;
import com.qimu.saiyou.service.JoinCompeteService;
import com.qimu.saiyou.service.TeamService;
import com.qimu.saiyou.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 加入竞赛
 *
 *@author: FeiXiang
 * 
 */
@RestController
@RequestMapping("/join_compete")
@Slf4j
public class JoinCompeteController {

    @Resource
    private JoinCompeteService joinCompeteService;

    @Resource
    private CompetitionService competitionService;

    @Resource
    private TeamService teamService;

    @Resource
    private UserService userService;

    /**
     * 加入竞争
     *
     * @param joinCompeteRequest 加入竞争请求
     * @param request            要求
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> joinCompete(@RequestBody JoinCompeteRequest joinCompeteRequest,
                                             HttpServletRequest request) {
        if (joinCompeteRequest == null || joinCompeteRequest.getCompetitionId() == null
                || joinCompeteRequest.getCompetitionId() <= 0 || joinCompeteRequest.getTeamId() == null
                || joinCompeteRequest.getTeamId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能操作
        final User loginUser = userService.getLoginUser(request);
        boolean result = joinCompeteService.doJoinCompete(joinCompeteRequest, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 退出竞争
     *
     * @param joinCompeteRequest 加入竞争请求
     * @param request            要求
     * @return {@link BaseResponse}<{@link Boolean}>
     */
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitCompete(@RequestBody JoinCompeteRequest joinCompeteRequest,
                                             HttpServletRequest request) {
        if (joinCompeteRequest == null || joinCompeteRequest.getCompetitionId() == null
                || joinCompeteRequest.getCompetitionId() <= 0 || joinCompeteRequest.getTeamId() == null
                || joinCompeteRequest.getTeamId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 登录才能操作
        final User loginUser = userService.getLoginUser(request);
        boolean result = joinCompeteService.doQuitCompete(joinCompeteRequest, loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 按页面列出我加入竞争
     * 获取我参与的竞赛列表
     *
     * @param request 要求
     * @return {@link BaseResponse}<{@link Page}<{@link CompetitionVO}>>
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<CompetitionVO>> listMyJoinCompeteByPage(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Page<Competition> competitionPage = joinCompeteService.listMyJoinCompeteByPage(loginUser);
        return ResultUtils.success(competitionService.getCompetitionVOPage(competitionPage, request));
    }
}
